Python DateTime weekday()用法及代码示例 | 您所在的位置:网站首页 › my weekday什么意思 › Python DateTime weekday()用法及代码示例 |
在本文中,我们将讨论 DateTime 模块中的 weekday() 函数。 weekday() 函数用于根据给定的 DateTime 获取周数。它将返回 0-6 范围内的数字表示意义0Monday1Tuesday2Wednesday3Thursday4Friday5Saturday6Sunday 它将以“(YYYY, MM, DD, HH, MM, SS)”的格式将输入作为DateTime,其中, YYYY 代表年份MM 代表月DD 代表日期HH 代表小时MM代表分钟SS 代表秒我们首先需要导入 DateTime 模块并创建一个 DateTime,现在使用 weekday() 函数我们将获得特定 DateTime 的工作日。 用法: datetime(YYYY,MM,DD, HH,MM,SS).weekday() 我们还可以使用以下语法从 DateTime 中提取日期: 用法: datetime(YYYY,MM,DD, HH,MM,SS).date() 示例:Python程序来创建DateTime并显示DateTime和日期 Python3 # importing datetime class from datetime import datetime # create datetime x = datetime(2021, 8, 8, 12, 5, 6) # display print("Datetime is:", x) # get the date print("Date is:", x.date())输出: Datetime is:2021-08-08 12:05:06 Date is:2021-08-08 例:用于获取给定日期时间的工作日的 Python 程序 Python3 # importing datetime class from datetime import datetime # create datetime x = datetime(2021, 8, 8, 12, 5, 6) # display print("Datetime is:", x) # get the weekday print("weekday is:", x.weekday()) # create datetime x = datetime(2021, 9, 10, 12, 5, 6) # display print("Datetime is:", x) # get the weekday print("weekday is:", x.weekday()) # create datetime x = datetime(2020, 1, 8, 12, 5, 6) # display print("Datetime is:", x) # get the weekday print("weekday is:", x.weekday())输出: Datetime is:2021-08-08 12:05:06 weekday is:6 Datetime is:2021-09-10 12:05:06 weekday is:4 Datetime is:2020-01-08 12:05:06 weekday is:2 范例3:获取工作日名称的Python程序 Python3 # create a list of weekdays from datetime import datetime days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] # importing datetime class # create datetime x = datetime(2021, 8, 8, 12, 5, 6) # display print("Datetime is:", x) # get the weekday name using index print("weekday is:", days[x.weekday()]) # create datetime x = datetime(2021, 9, 10, 12, 5, 6) # display print("Datetime is:", x) # get the weekday name using index print("weekday is:", days[x.weekday()]) # create datetime using index x = datetime(2020, 1, 8, 12, 5, 6) # display print("Datetime is:", x) # get the weekday name using index print("weekday is:", days[x.weekday()])输出: Datetime is:2021-08-08 12:05:06 weekday is:Sunday Datetime is:2021-09-10 12:05:06 weekday is:Friday Datetime is:2020-01-08 12:05:06 weekday is:Wednesday |
CopyRight 2018-2019 实验室设备网 版权所有 |